/*  This file is part of Real Time Digital Audio Processing library.
 *  Copyright (C) 2008 - Emilio Monti - emilmont@gmail.com
 *
 *  Real Time Digital Audio Processing library is free software: you can
 *  redistribute it and/or modify it under the terms of the GNU General
 *  Public License as published by the Free Software Foundation, either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  Real Time Digital Audio Processing library is distributed in the hope
 *  that it will be useful, but WITHOUT ANY WARRANTY; without even the
 *  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *  See the GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Real Time Audio Processing library.
 *  If not, see <http://www.gnu.org/licenses/>.
 */
#include <stddef.h>
#include <string.h>
#include <math.h>

#include "Generate.h"

Table::Table(unsigned int length) {
    this->length = length;
    data = new float[length];
    phase = 0;
}

Table::~Table() {
    delete [] data;
    data = NULL;
}

Sine::Sine(unsigned int sampleRate, float amp, unsigned int freq) {
    sprintf(descr, "[+] Sine(%uHz)", freq);

    /* Store the data of a complete period in a table */
    table = new Table((float)sampleRate/(float)freq);
    for(unsigned int i=0; i<table->length; i++)
        table->data[i] = amp * sin(((float)i/(float)table->length) * 2.*PI);
}

void Sine::processSignal(float* signal, unsigned long length) {
    for (unsigned int i=0; i<length; i++) {
        (*signal++) += table->data[table->phase];

        table->phase++;
        if (table->phase >= table->length)
            table->phase -= table->length;
    }
}

Sine::~Sine() {
delete table;
}
